feat(openapi-fetch): enable middleware request param module augmentation#2527
feat(openapi-fetch): enable middleware request param module augmentation#2527drwpow merged 1 commit intoopenapi-ts:mainfrom
Conversation
👷 Deploy request for openapi-ts pending review.Visit the deploys page to approve it
|
|
drwpow
left a comment
There was a problem hiding this comment.
This makes sense! Little PRs like this that don’t fundamentally change things, just provide little cleanups like this are always fine. Thank you!
Wait, I can't seem to fully get this working... declare module 'openapi-fetch' {
interface MiddlewareRequestParams {
timeout?: number;
}
}I can write a timeout middleware like this: // Timeout middleware
fetchClient.use({
async onRequest({ request, params: { timeout } }) {
const signal =
timeout === Infinity ?
request.signal
: AbortSignal.any([
request.signal,
AbortSignal.timeout(typeof timeout === 'number' && timeout >= 0 ? timeout : REQUEST_TIMEOUT),
]);
return new Request(request, { signal });
},
});But I can't really use my custom parameter because TypeScript will yell at me if I try to do this: client['/api/takes-a-while'].PUT({ params: { timeout: 120_000 } })
Did you somehow get around this without telling TypeScript to ignore this line (or was this just not your use case)? |
Nooo, I was working on it in November and I had to circumvent it 😅 I never found a way to require the param on the calling side. I used the |

Changes
Decided to open PR in lieu of issue.
I just changed the
.d.tsfile manually and extracted theMiddlewareRequestParamsinto their own interface. It opens up the possibility for using TS module augmentation to add parameters to theparamsproperty and use those in the middleware.My use-case is changing authentication logic based on a variable that isn't passed through to the API being invoked.
How to Review
Look at the file, I guess? 🤗
Also, I would be open to going deeper in a follow-up PR to look into how the client facing types could mirror the custom parameter added, if this is indeed an avenue you're open to exploring.
Checklist
(skipped all since it's just a small TS update and I didn't pull to local)
docs/updated (if necessary)pnpm run update:examplesrun (only applicable for openapi-typescript)